package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.User;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * 质量检查中的添加照片适配器
 */
public class AccidentAddPersonAdapter extends
        RecyclerView.Adapter {
    private Context context;
    private List<User> users;
    private OnRecyclerViewClickListener onRecyclerViewClickListener;
    private boolean isEdit;

    public interface OnRecyclerViewClickListener extends View.OnClickListener {
        void onItemClick(View v, int position);
    }

    public AccidentAddPersonAdapter(Context context, List<User> users, OnRecyclerViewClickListener onRecyclerViewClickListener, boolean isEdit) {
        this.context = context;
        this.users = users;
        this.onRecyclerViewClickListener = onRecyclerViewClickListener;
        this.isEdit = isEdit;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_addtest_person, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder) {
            final User user = users.get(position);
            final ViewHolder viewHolder = (ViewHolder) holder;
            if (user == null) {
                return;
            }
            if (user.getHeadPortrait() != null && Utils.ADDPIC.equals(user.getHeadPortrait())) {
                viewHolder.imageView.setImageResource(R.mipmap.circle_add);
                viewHolder.nameTv.setText(user.getName());
                viewHolder.delImageView.setVisibility(View.GONE);
            } else {
                if (isEdit) {
                    viewHolder.delImageView.setVisibility(View.VISIBLE);
                }
                String headPortrait = user.getHeadPortrait();
                if(!Utils.isEmpty(headPortrait)){
                    headPortrait = headPortrait.substring(0, headPortrait.lastIndexOf(".")) + "_100x100" + headPortrait.substring(headPortrait.lastIndexOf("."));
                }
                viewHolder.nameTv.setText(user.getName());
                Glide.with(context)
                        .load(headPortrait)
                        .placeholder(R.mipmap.icon_avater_defalut)
                        .dontAnimate()
                        .error(R.mipmap.icon_avater_defalut)
                        .into(viewHolder.imageView);
//                Glide.with(context).load(Utils.SERVER_PIC + user.getHeadPortrait()).dontAnimate().error(R.mipmap.icon_avater_defalut).into(viewHolder.imageView);
            }
            viewHolder.imageView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    onRecyclerViewClickListener.onItemClick(v, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView imageView;
        private final ImageView delImageView;
        private final HXTextView nameTv;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.item_imageview);
            delImageView = (ImageView) itemView.findViewById(R.id.del_iv);
            nameTv = (HXTextView) itemView.findViewById(R.id.name_tv);
        }
    }

}
